package Client;

import SQL.Connections;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.util.*;

public class Login extends JFrame{
    private static JFrame frameglobal;
    private JPanel panel1;
    private JLabel trainImage;
    private JButton 登录Button;
    private JButton 注册Button;
    private JPasswordField passwordField;
    private JTextField userField;
    private JLabel infoLabel;//提示信息标签

    private static String user1;
    private Connection con;
    private Statement stm;
    private ResultSet rs;
    private Connections connections = new Connections();


    public Login() {

        //按下注册按钮就跳转到注册窗口
        注册Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //创建并初始化注册界面
                Signup.initialize();
                //关闭登录界面
                frameglobal.dispose();
            }
        });

        //登录按钮
        登录Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String user = userField.getText();
                String password = passwordField.getText();

                login(user, password);

            }
        });


        //添加键盘监听事件 按enter可以直接登录
        passwordField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                super.keyTyped(e);
                if (e.getKeyChar() == '\n') {
                    String user = userField.getText();
                    String password = passwordField.getText();
                    user1=user;
                    login(user, password);

                }
            }
        });



    }


    //从数据库获取数据，登录
    public void login(String user, String password){
        try{
            Connection con = connections.getConnection();
            stm = con.createStatement();
            //用手机号登录
            rs = stm.executeQuery("SELECT * FROM usertrain WHERE UserPhoneNumber='"+user+"'");

            while (rs.next()) {
                // 获取数据库信息
                String id1 = rs.getString("UserPhoneNumber").toString().trim();
                String password1 = rs.getString("UserPassword").toString().trim();
                boolean isAdmin = rs.getBoolean("UserType");

                //验证登录信息
                if(id1.equals(user) && password1.equals(password)){
                    if(isAdmin){//管理员用户,跳转到管理员界面
                        //创建并初始化管理员界面
                        AdminPage.initialize();
                        //关闭登陆界面
                        frameglobal.dispose();
                    }else {//普通用户，跳转到普通用户界面
                        //创建并初始化用户界面
                        PassengerHome.initialize(user);
                        //关闭登陆界面
                        frameglobal.dispose();
                    }



                }else if(id1.equals(user)){

                    infoLabel.setText("密码错误");
                }
            }

            rs.close();
            stm.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
            infoLabel.setText("没有此用户");//这句话放的位置不对
        }


    }

    public static void  main(String[] args) {
        initialize();
    }

    public static void initialize() {
        JFrame frame = new JFrame("Login");
        frame.setContentPane(new Login().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);//设置屏幕居中
        frame.setVisible(true);
        frameglobal=frame;
    }

    private void createUIComponents() {
        //组件个性化
        // TODO: place custom component creation code here
        trainImage = new JLabel(new ImageIcon("train.png"));//插入火车图片
    }


}
